﻿using ShiBieProject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShiBieProject
{
    class invoiceDetailVserificationMoneyEtc
    {
        public static bool detailVserification(List<invoiceOcrDetailedModel> invoiceOcrDetailedDtoList,  decimal mxmoneyTotal,  decimal mxtaxmoneyTotal) {
            decimal sumMoney = 0;
            decimal sumTaxMoney = 0;
            foreach (invoiceOcrDetailedModel iodm in invoiceOcrDetailedDtoList) {
                string mxMoney = iodm.moneyAmount;
                string mxTaxMoney = iodm.taxAmount;
                string mxTaxlv = iodm.detaileTaxRate;
                //1.判断是否为空，格式不正确
                bool istaxlvzero  =  InvoiceMoneysCheck.getTaxRate(mxTaxlv);
                if (istaxlvzero) {
                    iodm.detaileTaxRate = "0%";
                    mxTaxlv = "0%";
                    iodm.taxAmount = "0";
                    mxTaxMoney = "0";
                }
                decimal moneye;
                decimal taxmoneye;
                if ((!decimal.TryParse(mxMoney, out moneye)) || (!decimal.TryParse(mxTaxMoney, out taxmoneye)))
                {
                    return false;
                }
                string resaulttaxlv = AmountAndDetailsEtcMoney.calculatingTaxRate(moneye, taxmoneye);
                if (string.IsNullOrEmpty(resaulttaxlv)) {
                    iodm.detaileTaxRateState = CompanyOrMoneyStateEnum.TaxLvIncorrect;
                }
                if (resaulttaxlv!= mxTaxlv) {

                    if (resaulttaxlv == "" || resaulttaxlv == null)
                    {
                        string taxlvfax= mxTaxlv.Replace("%", "");
                        decimal taxlv;
                        decimal.TryParse(taxlvfax, out taxlv);
                        taxlvfax = InvoiceOcrTool.setTaxLv(taxlv);
                        iodm.detaileTaxRate = taxlvfax;
                    }
                    else
                    {
                        iodm.detaileTaxRate = resaulttaxlv;
                    }
                    iodm.detaileTaxRateState = CompanyOrMoneyStateEnum.TaxLvIncorrect;
                }
                if (CheackIsNullOrEmpty(mxMoney, mxTaxMoney))
                {
                    return false;
                }
                sumMoney += moneye;
                sumTaxMoney += taxmoneye;
            }
            if (sumMoney == mxmoneyTotal && sumTaxMoney == mxtaxmoneyTotal)
            {
                return true;
            }
            else {
                return false;
            }
          
        }


        private static bool CheackIsNullOrEmpty(string money, string taxmoney)
        {
            if ((string.IsNullOrEmpty(money)) || (string.IsNullOrEmpty(taxmoney)))
            {
                return true;
            }
            return false;
        }
    }
}
